#include<iostream>
#include<unistd.h>
#include<pthread.h>
using namespace std;

int cnt = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* Count(void* args)
{
    pthread_detach(pthread_self());
    uint64_t number = (uint64_t)args;
    cout << "pthread: " << number << " create success " << endl;

    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        cout << "pthread: " << number << "  , cnt: " << cnt++ << endl;

        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    for (uint64_t i = 0; i < 5; i++)
    {
        pthread_t tid;
        pthread_create(&tid, nullptr, Count, (void*)i); // 传值传参
    }
    
    while(true) 
    {
        sleep(1);
        cout << "main thread ctrl begin:" << endl;
        pthread_cond_signal(&cond); // 唤醒cond 一个线程
        cout << "signal one thread...  " << endl;
    }
    return 0;
}